<?php

namespace app\api\controller\shop;


use app\admin\model\AssistantOrder;
use app\admin\model\Assistantprofit;
use app\admin\model\Shop;
use app\common\controller\Api;
use app\common\model\Area;


/**
 *
 */
class Assistant extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    protected $app = null;
    protected $appId="wxd286130b28614c62";

    public function index()
    {

        $date = $this->request->param('date');
        if($date == '' || $date == 1){
            $date = 'today';
        }elseif ($date == 2){
            $date = 'yes';
        }elseif ($date == 3){
            $date = 'month';
        }

        $user = $this->auth->getUser();
        $assistAntModel = new \app\admin\model\Assistant();
        $assistant = $assistAntModel->where('user_id',$user['id'])->where('status','verifyed')->find();

        $assistOrderModel =new AssistantOrder();

        $total = 0;
        $order = 0;
        $wait  = 0;
        if($date == 'today'){
            $total = $assistOrderModel->where('assistant_id',$assistant['id'])->whereTime('create_time','today')->
            where('status','not in',['paying','refund'])->sum('amount');
            $order = $assistOrderModel->where('assistant_id',$assistant['id'])->whereTime('create_time','today')->
            where('status','not in',['paying','refund'])->count('id');
            $wait = $assistOrderModel->where('assistant_id',$assistant['id'])->whereTime('create_time','today')->
            where('status','not in',['payed'])->count('id');
        }else if($date == 'yes'){
            $total = $assistOrderModel->where('assistant_id',$assistant['id'])->whereTime('create_time','yesterday')->
            where('status','not in',['paying','refund'])->sum('amount');
            $order = $assistOrderModel->where('assistant_id',$assistant['id'])->whereTime('create_time','yesterday')->
            where('status','not in',['paying','refund'])->count('id');

            $wait = $assistOrderModel->where('assistant_id',$assistant['id'])->whereTime('create_time','yesterday')->
            where('status','not in',['payed'])->count('id');
        }else if($date == 'month'){
            $total = $assistOrderModel->where('assistant_id',$assistant['id'])->whereTime('create_time','yesterday')->
            where('status','not in',['paying','refund'])->sum('month');
            $order = $assistOrderModel->where('assistant_id',$assistant['id'])->whereTime('create_time','yesterday')->
            where('status','not in',['paying','refund'])->count('id');

            $wait = $assistOrderModel->where('assistant_id',$assistant['id'])->whereTime('create_time','yesterday')->
            where('status','not in',['payed'])->count('id');
        }


        $ret = array();
        $ret['total'] = $total;
        $ret['order'] = $order;
        $ret['wait'] = $wait;

        $this->success('',$ret);

    }


    public function takeDetail()
    {

        $user = $this->auth->getUser();
        $assistAntModel = new \app\admin\model\Assistant();
        $assistant = $assistAntModel->where('user_id',$user['id'])->where('status','verifyed')->find();
        if(!$assistant)$this->error("还在审核中");
        $ret = array();
        $ret['price'] = $assistant['price'];
        $ret['address'] = $assistant['address'];
        $ret['is_open'] = $assistant['is_open'];
        $ret['time'] = $assistant['time'];
        $ret['time2'] = $assistant['time2'];
        $ret['lat'] = $assistant['lat'];
        $ret['lng'] = $assistant['lng'];
        $ret['mobile'] = $assistant['mobile'];

        $this->success('',$ret);
    }

    public function updateSet()
    {
        $user = $this->auth->getUser();
        $assistAntModel = new \app\admin\model\Assistant();
        $assistant = $assistAntModel->where('user_id',$user['id'])->where('status','verifyed')->find();
        if(!$assistant)$this->error("还在审核中");

        $address = $this->request->param('address');
        $lat = $this->request->param('lat');
        $lng = $this->request->param('lng');
        $city = $this->request->param('city');
        $is_open = $this->request->param('is_open');
        $time = $this->request->param('time');
        $time2 = $this->request->param('time2');
        $mobile = $this->request->param('mobile');

        $db = array();
        $db['lat'] = $lat;
        $db['lng'] = $lng;
        $db['address'] = $address;
        $db['city'] = $city;
        $db['is_open'] = $is_open;
        $db['time'] = $time;
        $db['time2'] = $time2;
        $db['mobile'] = $mobile;

        $assistAntModel->where('id',$assistant['id'])->update($db);

        $this->success('成功');

    }

     public function detail()
     {

         $user = $this->auth->getUser();
         $assistAntModel = new \app\admin\model\Assistant();
         $assistant = $assistAntModel->where('user_id',$user['id'])->where('status','verifyed')->find();

         $assistant['logo'] = cdnurl($assistant['logo'],true);
         $images = explode(",",$assistant['image']);
         $img = array();
         foreach ($images as $key=>$val){
             $img[] = cdnurl($val,true);
         }
         $assistant['image'] = $img;
         $tag = explode(",",$assistant['tag']);
         $assistant['tag'] = $tag;

         $this->success('',$assistant);
     }


    public function add()
    {

        $user = $this->auth->getUser();
        $name = $this->request->param('name');
        $mobile = $this->request->param('mobile');
        $sex = $this->request->param('sex');
        $birth = $this->request->param('birth');
        $address = $this->request->param('address');
        $lat = $this->request->param('lat');
        $lng = $this->request->param('lng');
        $type = $this->request->param('type');
        $level = $this->request->param('level');
        $time1 = $this->request->param('time1');
        $time2 = $this->request->param('time2');
        $intro = $this->request->param('intro');
        $logo = $this->request->param('logo');
        $cover = $this->request->param('cover');
        $tag = $this->request->param('tag');
        $city = $this->request->param('city');
        $price = $this->request->param('price');
        $tag = json_decode(htmlspecialchars_decode($tag), true);
        $cover = json_decode(htmlspecialchars_decode($cover), true);

        if($sex == 2){
            $sex = "girl";
        }else{
            $sex = "boy";
        }
        if($level == 1){
            $level = "萌新";
        }else if($level == 2){
            $level = "业余";
        }else{
            $level = "专业";
        }
        $tagStr = "";
        foreach ($tag as $key=>$val){
            $tagStr = $tagStr.$val;
            if($key != count($tag)-1){
                $tagStr = $tagStr.",";
            }
        }

        $coverStr = "";
        foreach ($cover as $key=>$val){
            $val =  str_replace("https://img.quexiaqp.com", "", $val);
            $coverStr = $coverStr.$val;
            if($key != count($cover)-1){
                $coverStr = $coverStr.",";
            }
        }
        $logo = str_replace("https://img.quexiaqp.com", "", $logo);
        $assistantModel = new \app\admin\model\Assistant();
        $db = array();
        $db['user_id'] = $user['id'];
        $db['name'] = $name;
        $db['mobile'] = $mobile;
        $db['sex'] = $sex;
        $db['type'] = $type;
        $db['level'] = $level;
        $db['birth'] = $birth;
        $db['logo'] = $logo;
        $db['image'] = $coverStr;
        $db['intro'] = $intro;
        $db['tag'] = $tagStr;
        $db['lat'] = $lat;
        $db['lng'] = $lng;
        $db['address'] = $address;
        $db['city'] = $city;
        $db['price'] = $price;
        $db['time'] = $time1;
        $db['time2'] = $time2;
        $db['status'] = 'verifying';

        $assistant = $assistantModel->where('user_id',$user['id'])->where('status','verifyed')->find();

        $assistantModel->where('id',$assistant['id'])->update($db);

        $this->success('成功');



    }


    public function orderList()
    {

        $page = $this->request->param('page');
        $status = $this->request->param('status');

        $user = $this->auth->getUser();

        $where = array();
        if($status != ""){

            //'paying','payed','taked','doing','done','refund','cancel'
            $where['status'] = $status;
        }

        $assistAntModel = new \app\admin\model\Assistant();
        $assistant = $assistAntModel->where('user_id',$user['id'])->where('status','verifyed')->find();


        $limit = 10;
        $offset = ($page - 1) * $limit;
        $assistOrderModel =new AssistantOrder();
        $list = $assistOrderModel->where('assistant_id',$assistant['id'])->where('status','<>','paying')->where($where)->limit($offset
        ,$limit)->order('create_time','desc')->select();

        $shopModel = new \app\admin\model\Shop();
        $userModel = new \app\admin\model\User();
        foreach ($list as $key=>$val){
            $list[$key]['order_no_str'] = substr($val['order_no'],0,5)."*****".substr($val['order_no'],strlen($val['order_no'])-4,4);
            $list[$key]['status_txt'] = $assistOrderModel->getStatusTxt($val['status']);

            $shop  = $shopModel->where('id',$val['shop_id'])->find();
            $list[$key]['shop'] = $shop;
            $user = $userModel->where('id',$val['user_id'])->find();
            $list[$key]['start_time_txt'] = date("Y-m-d H:i",$val['start_time']);
            $list[$key]['end_time_txt'] = date("Y-m-d H:i",$val['end_time']);
            $list[$key]['mobile'] = $user['mobile'];



        }
        $this->success('',$list);

    }


    public function updateStatus()
    {

        $order_no = $this->request->param('order_no');
        $status = $this->request->param('status');
        $assistOrderModel =new AssistantOrder();
        $order = $assistOrderModel->where('order_no',$order_no)->find();
        if(!$order) $this->error("订单不存在");
        $user = $this->auth->getUser();

        $assistAntModel = new \app\admin\model\Assistant();
        $assistant = $assistAntModel->where('user_id',$user['id'])->where('status','verifyed')->find();

        if($order['assistant_id'] != $assistant['id']) $this->error("无权限操作");

        $assistOrderModel->where('id',$order['id'])->update(['status'=>$status]);

        if($status == 'taked'){
            $assistantProfitModel = new Assistantprofit();
            $db = array();
            $db['user_id'] = $user['id'];
            $db['amount'] = $order['pay_amount']-$order['pay_amount']*0.006;
            $db['create_time'] = time();
            $db['status'] = -1;
            $db['order_id'] = $order['id'];

            $assistantProfitModel->insert($db);
        }

        $this->success('成功');

    }

    public function orderDetail()
    {

        $order_no = $this->request->param('order_no');
        $assistantOrderModel = new AssistantOrder();
        $order = $assistantOrderModel->where('order_no',$order_no)->find();
        if(!$order) $this->error("订单不存在");
        $shopModel = new Shop();
        $shop = $shopModel->where('id',$order['shop_id'])->find();
        $order['shop'] = $shop;


        $assistAntModel = new \app\admin\model\Assistant();
        $assistant = $assistAntModel->where('id',$order['assistant_id'])->find();
        $assistant['logo'] = cdnurl($assistant['logo'],true);
        $order['assistant'] = $assistant;

        $hour = ($order['end_time']- $order['start_time'])/3600;
        $order['hour'] = $hour;

        $this->success('',$order);
    }


}
